﻿using System;
using System.Data.Entity;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using WeBlog.Migrations;
using WeBlog.Models;

namespace WeBlog
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Forces initialization of database on model changes.
            using (var context = new BlogDbContext())
            {
                try
                {
                    context.Database.Connection.Open();
                    context.Database.Connection.Close();
                }
                catch 
                {
                    Database.SetInitializer<BlogDbContext>(new DropCreateDatabaseAlways<BlogDbContext>());
                    context.Database.Initialize(force: true);                    
                }
                
            }
        }
    }


}
